/* 
Copyright (c) 2004,2005, 2006, 2007, 2008 University of Illinois 

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
AccessibilityExtension.ns(function() { with (AccessibilityExtension.LIB) {

	var AE = AccessibilityExtension.ns;  
	AE.dialogName = "accessext-form-list-events";	

	var _events=new Array("onblur","onclick","ondblclick","onfocus","onkeydown","onkeypress","onkeyup",
		"onmousedown","onmouseup","onmousemove","onmouseout","onmouseover","onresize","onload", "onchange");
	
	function singleEventObjectDisplayDetails() {}
	singleEventObjectDisplayDetails.prototype = {
		dataElementNames : ['order','event'],
		columnHeaders : ['column.header.number.label','column.header.event.label'],
		
		accessIssuesDataElementNames : [],
		accessIssuesMessages : [],
	}
	var singleEventObjectDisplayDetails = new singleEventObjectDisplayDetails();
		
	function singleEventObject(order, event, action) {
		this.order = order;
		this.event = event;
		this.action = action;
		this.parseNode(null);//for future use
	}
	singleEventObject.prototype = {
		order : null,
		event : null,
		action : null,
		display : true,
		displayDetails: singleEventObjectDisplayDetails,
	 	parseNode : function (node) {
			//TODO: Does this need to be defined?
		},
		getTreeNode : function(windoc) {
			return accesxt_generateTreeboxResults(windoc,this);
		}
	}
	
	function eventObjectDisplayDetails() {}
	eventObjectDisplayDetails.prototype = {
		dataElementNames : ['icita07', 'icita08','order', 'baseuri','tagName','text'],
		columnHeaders : ['column.header.icita07.label','column.header.icita08.label','column.header.order.label','column.header.baseuri.label','column.header.tag.label', 'column.header.text.label'],
		
		accessIssuesDataElementNames : ['missingonfocus','missingonblur', 'nonfocusableonclick', 'nonfocusableonmouse', 'selectonchange', 'missingkeyequiv'],
		accessIssuesMessages : ['events.warnings.missingonfocus', 'events.warnings.missingonblur', 'events.msg.nonfocusableonclick', 'events.msg.nonfocusableonmouse', 'events.msg.selectonchange', 'events.msg.missingkeyequiv'],
	}
	var eventObjectDisplayDetails = new eventObjectDisplayDetails();
	
	function eventObject(torder,tnode) {
		this.order = torder;
		this.parseNode(tnode);
	}
	eventObject.prototype = {
		icita07 : "",
		icita08 : "",
		node : null,
		baseuri : "",
		events : null,
		tagName : null,
		text : null,
		alt : null,
		missingonfocus : false,
		missingonblur : false, 
		nonfocusableonclick : false,
		nonfocusableonmouse : false,
		selectonchange : false,
		missingkeyequiv : false,
		display : true,
		displayDetails: eventObjectDisplayDetails,
		is_focusable : function() {
			function isInteger(sText)	{
				var ValidChars = "0123456789";
				if (sText.length == 0) return false;
				for (i = 0; i < sText.length; i++) { 
					if (ValidChars.indexOf(sText.charAt(i)) == -1) return false;
				}
				return true;
			}		
			var focusableElements = ['input','select','button','textarea','option','area'];
			if (this.node.nodeName.toLowerCase() == "a" && typeof this.node.href != "undefined") return true;
			if (this.node.nodeName.toLowerCase() == "area" && typeof this.node.href != "undefined") return true;
			if (this.node.hasAttribute('tabindex') && isInteger(this.node.getAttribute('tabindex'))) return true;
			if (focusableElements.indexOf(this.node.nodeName.toLowerCase()) != -1) return true;		
			return false;
		},
		parseNode : function(node) {
			assert(node != null);
			this.node = node;
			this.baseuri = getBaseURI(node);
	
			this.tagName = node.tagName;
			if (this.node.tagName.toLowerCase() != 'body') {
				this.text = GetTextOnlyFromNode(node);
			} else {
				this.text = "";
			}
			this.alt = GetUserAltFromNode(node);
	
			this.events = new Array();
			var i;
			var focusable = this.is_focusable();
			var eventNames = new Array();
			
			for(i in _events) 	{
				var e = _events[i];
				if (this.node.hasAttribute(e)) {
					var attrValue = cleanSpaces(GetValueFromAttributes(this.node,[e],""));
					this.events.push(new singleEventObject(this.events.length + 1,e,attrValue));
					if (attrValue != '') eventNames.push(e);
				}
			}
			if (focusable) {
				if (eventNames.indexOf("onmouseover") != -1 && eventNames.indexOf("onfocus") == -1) this.missingonfocus = true;	
				if (eventNames.indexOf("onmouseout") != -1 && eventNames.indexOf("onblur") == -1)  this.missingonblur = true;	
			} else {
				if (eventNames.indexOf("onclick") != -1) this.nonfocusableonclick = true;	
				if (eventNames.indexOf("onmouseover") != -1 || eventNames.indexOf("onmouseout") != -1){ this.nonfocusableonmouse = true; }			
			}
			if (this.node.nodeName.toLowerCase() == "select" && eventNames.indexOf("onchange") != -1 ){ this.selectonchange = true; }
			if (eventNames.indexOf("onmousedown") != -1) this.missingkeyequiv = true;	
			if (eventNames.indexOf("onmouseup") != -1) this.missingkeyequiv = true;	
			if (eventNames.indexOf("onmousemove") != -1) this.missingkeyequiv = true;	
			
			//	setMaxSeverityForArray(loadArray);
			this.icita07 = getMaxSeverity(this, 'icita07');		
			this.icita08 = getMaxSeverity(this, 'icita08');		
		},
		getTreeNode : function(windoc) {
			return accesxt_generateTreeboxResults(windoc,this);
		},
		listDetailsInHTMLFormat : function () {
			var html = "";
			for (var findex = 0; findex < this.events.length; findex ++) { //for each treerow
				html = html + "<li>" + this.events[findex].event + "</li>"  ;
			}
			if (html != "") { html = "<dl><dt><strong>Event(s):</strong></dt><dd><ul> " + html + "</ul></dd></dl>" }
			else {html = '&nbsp;'}
			return html;
		}
	}
AccessibilityExtension.LIB.extend(AccessibilityExtension.LIB,
{
	newEventObject: function(torder,tnode) { 
		var n = new eventObject(torder,tnode);
		return n; 
	},

	accessext_handle_treebox_main: function() {
		if(AE.treebox_main.currentIndex < 0 || AE.treebox_main.currentIndex >= AE.primaryarray.length) {
			return;
		}
	
		var findex = AE.treebox_main.currentIndex;
		var node = AE.primaryarray[findex].node;
		if(node == null || AE.mydoc == null)
			return;
		accessext_scrollAndHighlightNode(node);
		
		AE.dataarray = AE.primaryarray[findex].events;
				
		accessext_setNoteText(findex);
	
		UpdateTreeView();
	},
	
	UpdateTreeViewMain: function() {
		var sc = document.getElementById(AE.sortColumn_main);
		if (sc == null) {
			accessext_consoleDump("Could not find column: " + AE.sortColumn);
			return;
		}
	//Clear the children and create a new one
		var treeChildren = accessext_clearTreeItems(AE.treebox_main);
		accessext_clearTreeItems(AE.treebox);
		//Sort
		//First clear all directions
		var cols = AE.treebox_main.getElementsByTagName("treecols");
		if (cols.length == 1) {
			for (var i = 0; i < cols[0].childNodes.length; i ++)
				cols[0].childNodes[i].setAttribute("sortDirection","");
		}
	
		sc.setAttribute("sortDirection", AE.sortDirectionAscending_main ? "ascending" : "descending");
		switch (AE.sortColumn_main) {
			case "list-header-icita07":
				AE.sortProperty="icita07";
			break;
			case "list-header-main-order":
				AE.sortProperty="order";
			break;
			case "list-header-main-tagName":
				AE.sortProperty="tagName";
			break;
			case "list-header-main-text":
				AE.sortProperty="text";
			break;
			default:
				AE.sortProperty="order";
			break;
		}
		
		AE.primaryarray.sort(accessext_sortColumnElements);
		
		//Finally add elements to the list
		var treeItem = null;
		for(var i = 0; i < AE.primaryarray.length; i ++) {
			treeItem = AE.primaryarray[i].getTreeNode(document);
			treeItem.setAttribute("value", i);
			treeChildren.appendChild(treeItem);
		}
	},
	
	UpdateTreeView: function() {
		var sc = document.getElementById(AE.sortColumn);
		if (sc == null) {
			accessext_consoleDump("Could not find column: " + AE.sortColumn);
			return;
		}
	//Clear the children and create a new one
		var treeChildren = accessext_clearTreeItems(AE.treebox);
		
		//Sort
		//First clear all directions
		var cols = AE.treebox.getElementsByTagName("treecols");
		if (cols.length == 1) {
			for (var i = 0; i < cols[0].childNodes.length; i ++)
				cols[0].childNodes[i].setAttribute("sortDirection","");
		}
	
		sc.setAttribute("sortDirection", AE.sortDirectionAscending ? "ascending" : "descending");
		switch (AE.sortColumn) {
			case "list-header-order":
				AE.sortProperty="order";
			break;
			case "list-header-event":
				AE.sortProperty="event";
			break;
			default:
				AE.sortProperty="order";
			break;
		}
		
		//Now this is a bit different
		
		AE.dataarray.sort(accessext_sortColumnElements);
		
		//Finally add elements to the list
		if(AE.treebox_main.currentIndex < 0 || AE.treebox_main.currentIndex >= AE.primaryarray.length || AE.treebox_main.currentIndex == null)
			return;
	
		var findex = AE.treebox_main.currentIndex;
		var node = AE.primaryarray[findex].node;
		if(node == null || AE.mydoc == null)
			return;
	
		var i;
		var treeItem = null;
		for (i in AE.dataarray) {
			var event = AE.dataarray[i];
			treeItem = event.getTreeNode(document);
			treeChildren.appendChild(treeItem);
		}
		return;
	},
	
	accessext_handle_listbox: function() {
		UpdateTreeView();
	},
	
	ParseDocument: function(doc) {
		ParseDocumentForEvents(doc, AE.primaryarray);
	},
	
	ParseDocumentForEvents: function(node, loadArray) {
		var i;
		var found = false;
		if(node.getAttribute) {
			for (i = 0; i < _events.length && !found; i ++) {
				if (node.hasAttribute(_events[i])) {
					found = true;
					loadArray[loadArray.length] = new eventObject(loadArray.length + 1, node);
				}
			}
		}
	
		if(node.childNodes) {
			for(var i = 0; i < node.childNodes.length; i ++)
				ParseDocumentForEvents(node.childNodes[i], loadArray);
		}
	},
	
	FireEvent: function() {
		var theindex = AE.treebox_main.currentIndex;
		var theevent = AE.treebox.currentIndex;
		CleanUp();
		if(theindex < 0 || theindex >= AE.primaryarray.length || theevent < 1 || theevent > 13)
			return false;
	
		var node = AE.primaryarray[theindex].node;
	
		var evt = AE.mydoc.createEvent("Events");
		var eventString="";
	
		//@todo : instead of a switch, just have an array
		theevent = AE.primaryarray[theindex].events[theevent];
		eventString = theevent.event.substr(2);
		evt.initEvent(eventString, true, true); 
		node.dispatchEvent(evt);
		return true;
	},
	
	post_InitAccessibilityExt: function() {
		accessext_setNoteText(0);
	}
});
	if (AccessibilityExtension.LIB.InitAccessibilityExt) AccessibilityExtension.LIB.InitAccessibilityExt();	
}});	